//
//  XZHHomeNoteViewController.m
//  Piece
//
//  Created by dllo on 16/6/12.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHHomeNoteViewController.h"
#import "XZHHomeTableView.h"
#import "XZHHomeTableViewCell.h"
#import "XZHHomeNoteTableViewCell.h"
#import "XZHHomeCollectionViewCell.h"
#import "DYJDataBaseTool.h"
#define SendPhotosViewControllerCamera 0
#define SendPhotosViewControllerPickPhoto 1
@interface XZHHomeNoteViewController ()<UITableViewDataSource, UITableViewDelegate, HomeNoteTableViewCellDelegate, UIImagePickerControllerDelegate, UIActionSheetDelegate, UINavigationControllerDelegate>
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation XZHHomeNoteViewController{
    UIImage *image;
    NSString *contentStr;
    NSString *dateStr;
    NSString *imageStr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTitle:@"小记"];
    
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.86 green:0.87 blue:0.88 alpha:1.00];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(didClickedLeftBarButton:)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(didClickedRightBarButton:)];
    [self setUpHomeDetailTableView];
}

- (void)didClickedLeftBarButton:(UIBarButtonItem *)sender {
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)didClickedRightBarButton:(UIBarButtonItem *)sender {
    
    NSData *data = UIImageJPEGRepresentation(image, 1.0f);
    imageStr = [data base64Encoding];
    
    
    DYJDataBaseTool *dataBaseTool = [DYJDataBaseTool shareDYJDataBaseTool];
    [dataBaseTool insertDataWithNoteContent:contentStr Image:imageStr Data:dateStr PlaceholderImage:_model.hp_img_url];
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)setUpHomeDetailTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 49 - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"XZHHomeNoteTableViewCell" bundle:nil] forCellReuseIdentifier:@"noteTableViewCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [tableView setContentOffset:CGPointMake(0, 0) animated:NO];
        XZHHomeNoteTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"noteTableViewCell"];
        cell.delegate = self;
        cell.model = self.model;
        cell.titlesLabel.text = @"From PIECE / Self";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        cell.backgroundColor = [UIColor clearColor];
        cell.userInteractionEnabled = NO;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

#pragma mark 选取照片代理方法
- (void)pickPhoto{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"请选择照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从手机相册选择", nil];
    [actionSheet showInView:self.view];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == SendPhotosViewControllerCamera) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *imagePickerConroller = [[UIImagePickerController alloc] init];
            imagePickerConroller.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePickerConroller.allowsEditing = NO;
            imagePickerConroller.showsCameraControls = YES;
            imagePickerConroller.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
            imagePickerConroller.delegate = self;
            [self presentViewController:imagePickerConroller animated:YES completion:nil];
        } else {
            NSLog(@"相机不可用");
        }
    } else if (buttonIndex == SendPhotosViewControllerPickPhoto) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            imagePickerController.allowsEditing = NO;
            imagePickerController.delegate = self;
            [self presentViewController:imagePickerController animated:YES completion:nil];
        } else {
            NSLog(@"相册不可用");
        }
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    image = info[UIImagePickerControllerOriginalImage];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"照片" object:image];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)passDate:(NSString *)date
{
    dateStr = date;
}

- (void)passContent:(NSString *)content
{
    contentStr = content;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        XZHHomeModel *model = self.model;
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName,nil];
        CGRect newRect = [model.hp_content boundingRectWithSize:CGSizeMake(ScreenWidth - 36, 8000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        return 420 + newRect.size.height + 55;
    } else {
        return 250;
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [[self.navigationController.navigationBar viewWithTag:1260] setHidden:YES];
    [[self.navigationController.navigationBar viewWithTag:1260] setUserInteractionEnabled:NO];
    [[self.navigationController.navigationBar viewWithTag:1261] setHidden:YES];
    [[self.navigationController.navigationBar viewWithTag:1261] setUserInteractionEnabled:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
